package com.youdoneed.community.shortVideo.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.youdoneed.community.common.vo.PageData;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.shortVideo.entity.Musiclibrary;
import com.youdoneed.community.shortVideo.entity.Musicrecommend;
import com.youdoneed.community.shortVideo.entity.Musictyperel;
import com.youdoneed.community.shortVideo.service.MusicrecommendService;
import com.youdoneed.community.shortVideo.vo.MusicTypeRelIdVo;
import com.youdoneed.community.shortVideo.vo.MusicTypeRelVo;
import com.youdoneed.community.shortVideo.vo.MusicTypeVo;
import com.youdoneed.community.shortVideo.vo.RecommendIdVo;
import com.youdoneed.community.shortVideo.vo.RecommendVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 音乐推荐模块控制器
 * @author lixs
 * 2017年8月31日
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/musicrecommend", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "推荐音乐模块控制器", description = "推荐音乐模块控制器")

public class MusicrecommendController {
	
	 private final Logger logger = LoggerFactory.getLogger(MusicrecommendController.class);
	 
	 
	 @Autowired
	 MusicrecommendService musicrecommendService;
	 
	 /**
	  * 向音乐分类中添加推荐音乐
	  * @param vo
	  * @author lixs
	  * @Date 2017.9.4
	  */
	 @ResponseBody
     @RequestMapping(value = "/addRecommendMusicToTypeBatch", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "向音乐分类中添加推荐音乐", value = "向音乐分类中添加推荐音乐")
	 public ResultMessage<Boolean> addRecommendMusicToTypeBatch(@RequestBody MusicTypeRelIdVo vo){
		
		logger.info("向音乐分类中添加推荐音乐");
		List<String> musicTypeRelId = vo.getMusicTypeRelId();
		Boolean result = musicrecommendService.addRecommendMusicToTypeBatch(musicTypeRelId);
		if(true==result){
			return ResultMessage.newSuccess("向音乐分类中添加推荐音乐成功").setData(result);
		}
		return ResultMessage.newFailure("向音乐分类中添加推荐音乐失败");
	}
	 
	 /**
	  * 查询分类下的推荐音乐
	  * @param typeId
	  * @author lixs
	  * @Date 2017.9.4
	  */
	 @ResponseBody
     @RequestMapping(value = "/findRecommendMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "查询分类下的推荐音乐", value = "查询分类下的推荐音乐")
	 public ResultMessage<PageData> findRecommendMusic(String typeId,Integer offset, Integer limit){
		 
		 
		 logger.info("查询分类下的推荐音乐");
		 List<RecommendVo> musiclibrary = musicrecommendService.findRecommendMusic(typeId,offset,limit);
		 
		 PageData page =new PageData();
		 page.setRows(musiclibrary);
		 page.setTotal(musicrecommendService.findRecommendMusicForTotal(typeId));
		 return ResultMessage.newSuccess("查询分类下的推荐音乐").setData(page);
		 
	 }
	 
	 /**
	  * 移除分类下的推荐音乐
	  * @param vo
	  * @author lixs
	  * @Date 2017.9.4
	  */
	 @ResponseBody
     @RequestMapping(value = "/removeRecommendMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "移除分类下的推荐音乐", value = "移除分类下的推荐音乐")
	 public ResultMessage<Boolean> removeRecommendMusic(@RequestBody RecommendIdVo vo){
		 
		 logger.info("移除分类下的推荐音乐");
		 List<String>  musicrecommendId = vo.getRecommendMusicTypeId();
		 String typeId = vo.getTypeId();
		 Boolean result = musicrecommendService.removeRecommendMusic(musicrecommendId,typeId);
		 if(true==result){
			 return ResultMessage.newSuccess("移除分类下的推荐音乐成功");
		 }
		 return ResultMessage.newFailure("移除分类下的推荐音乐失败");
	 }
	 
	 /**
	  * 查询分类下是否存在某一推荐音乐
	  * @param musicId
	  * @param typeId
	  * @return
	  */
	 @ResponseBody
     @RequestMapping(value = "/recommendUnique", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "查询分类下是否存在某一推荐音乐", value = "查询分类下是否存在某一推荐音乐")
	 public ResultMessage<Boolean> recommendUnique(String musicId,String typeId){
		 Boolean result = musicrecommendService.recommendUnique(musicId, typeId);
		 if(true==result){
		 return ResultMessage.newSuccess("查询成功").setData(result);
		 }
		 return ResultMessage.newFailure("查询失败");
	 }
	 
	 /**
	  * 通过歌手或者歌曲名查询推荐歌曲
	  * @param name
	  * @author lixs
	  * @Date 2017.7.20
	  */
	 @ResponseBody
     @RequestMapping(value = "/findRecommendMusicByName", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "通过歌手或者歌曲名查询推荐歌曲", value = "通过歌手或者歌曲名查询推荐歌曲")
	 public ResultMessage<PageData> findRecommendMusicByName(String name,Integer offset,Integer limit){
		 logger.info("通过歌手或者歌曲名查询推荐歌曲");
		 
		 List<Musiclibrary> musiclibrary = musicrecommendService.findRecommendMusicByName(name,offset,limit);
		 PageData page = new PageData();
		 page.setRows(musiclibrary);
		 page.setTotal(musicrecommendService.findRecommendMusicByNameForTotal(name));
		 return ResultMessage.newSuccess().setData(page);
	 }

	 /**
	  * 交换两个推荐音乐的顺序(箭头向上)
	  * @param musicrecommendId
	  * @author lixs
	  * @Date 2017.9.27
	  */
	 @ResponseBody
     @RequestMapping(value = "/exchangeSortTop", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "交换两个推荐音乐的顺序(箭头向上)", value = "交换两个推荐音乐的顺序(箭头向上)")
	 public ResultMessage<Integer> exchangeSortTop(String musicrecommendId){
		 
		 int result = musicrecommendService.exchangeSortTop(musicrecommendId);
		 if(0!=result){
			 return ResultMessage.newSuccess("交换两个推荐音乐的顺序成功").setData(result);
		 }
		 return ResultMessage.newFailure("交换两个推荐音乐的顺序失败");
	 }
	 
	 /**
	  * 交换两个推荐音乐的顺序(箭头向下)
	  * @param musicrecommendId
	  * @author lixs
	  * @Date 2017.10.20
	  */
	 @ResponseBody
     @RequestMapping(value = "/exchangeSortBehind", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "交换两个推荐音乐的顺序(箭头向下)", value = "交换两个推荐音乐的顺序(箭头向下)")
	 public ResultMessage<Integer> exchangeSortBehind(String musicrecommendId){
		 
		 int result = musicrecommendService.exchangeSortBehind(musicrecommendId);
		 if(0!=result){
			 return ResultMessage.newSuccess("交换两个推荐音乐的顺序成功").setData(result);
		 }
		 return ResultMessage.newFailure("交换两个推荐音乐的顺序失败");
	 }

	/**
	 * 置顶推荐的音乐
	 * @param musicrecommendId
	 * @return
	 */
	@RequestMapping(value = "/top", method = {RequestMethod.GET, RequestMethod.POST})
	@ApiOperation(notes = "置顶推荐音乐", value = "置顶推荐音乐")
	public ResultMessage<?> top(String musicrecommendId){
		musicrecommendService.top(musicrecommendId);
		return ResultMessage.newSuccess("置顶推荐音乐成功");
	}


	 /**
	  * 音乐播放后播放次数更新
	  * @param musicId
	  * @author lixs
	  * @Date 2017.9.28
	  */
	 @ResponseBody
     @RequestMapping(value = "/updatePlayCount", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "音乐播放后播放次数更新", value = "音乐播放后播放次数更新")
	 public ResultMessage<Integer> updatePlayCount(String musicId){
		 logger.info("音乐播放后播放次数更新");
		 int result = musicrecommendService.updatePlayCount(musicId);
		 if(0==result){
			 return ResultMessage.newFailure("音乐播放后播放次数更新失败");
		 }
		 return ResultMessage.newSuccess("音乐播放后播放次数更新成功");
	 }
	 
	 /**
	  * 向首页音乐库中添加推荐音乐
	  * @param vo
	  * @author lixs
	  * @Date 2017.10.11
	  */
	 @ResponseBody
     @RequestMapping(value = "/addMusicLibraryRecommend", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "向首页音乐库中添加推荐音乐", value = "向首页音乐库中添加推荐音乐")
	 public ResultMessage<Boolean> addMusicLibraryRecommend(@RequestBody MusicTypeVo vo){
		
		logger.info("向首页音乐库中添加推荐音乐");
		List<String> musicId = vo.getMusicId();
		Boolean result = musicrecommendService.addMusicLibraryRecommend(musicId);
		if(true==result){
			return ResultMessage.newSuccess("向首页音乐库中添加推荐音乐成功");
		}
		return ResultMessage.newFailure("向首页音乐库中添加推荐音乐失败");
	}
	 
	 /**
	  * 查询首页热点音乐分类下的推荐音乐
	  * @author lixs
	  * @Date 2017.10.11
	  */
	 @ResponseBody
     @RequestMapping(value = "/findLibraryRecommendMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "查询首页热点音乐分类下的推荐音乐", value = "查询首页热点音乐分类下的推荐音乐")
	 public ResultMessage<PageData> findLibraryRecommendMusic(Integer offset, Integer limit){
		 
		 logger.info("查询首页热点音乐分类下的推荐音乐");
		 List<RecommendVo> musiclibrary = musicrecommendService.findLibraryRecommendMusic(offset, limit);
		 
		 PageData page =new PageData();
		 page.setRows(musiclibrary);
		 page.setTotal(musicrecommendService.findMusiclibraryRecommendMusicForTotal());
		 return ResultMessage.newSuccess("查询首页热点音乐分类下的推荐音乐成功").setData(page);

	 }
	 
	 /**
	  * 移除首页热点音乐分类下的推荐音乐
	  * @param vo
	  * @author lixs
	  * @Date 2017.10.11
	  */
	 @ResponseBody
     @RequestMapping(value = "/removeLibraryRecommendMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "移除首页热点音乐分类下的推荐音乐", value = "移除首页热点音乐分类下的推荐音乐")
	 public ResultMessage<Boolean> removeLibraryRecommendMusic(@RequestBody RecommendIdVo vo){
		 
		 logger.info("移除首页热点音乐分类下的推荐音乐");
		 List<String>  musicrecommendId = vo.getRecommendMusicTypeId();
		 Boolean result = musicrecommendService.removeLibraryRecommendMusic(musicrecommendId);
		 if(true==result){
			 return ResultMessage.newSuccess("移除分类下的推荐音乐成功");
		 }
		 return ResultMessage.newFailure("移除分类下的推荐音乐失败");
	 }
	 
	/**
	 * 随机抽取一首推荐音乐
	 * @author lixs
	 * @Date 2017.10.23
	 */
	 @ResponseBody
     @RequestMapping(value = "/findRandomMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "随机抽取一首推荐音乐", value = "随机抽取一首推荐音乐")
	 public ResultMessage<RecommendVo> findRandomMusic(){
		 return ResultMessage.newSuccess("随机抽取一首推荐音乐成功").setData(musicrecommendService.findRandomMusic());
	 }
	 
	 /**
	  * 通过名字查询推荐音乐(后台页面)
	  * @param typeId
	  * @param name
	  * @param offset
	  * @param limit
	  * @author lixs
	  * @Date 2017.11.8
	  */
	 @ResponseBody
     @RequestMapping(value = "/findRecommendMusicByNameAndTypeId", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "通过名字查询推荐音乐(后台页面)", value = "通过名字查询推荐音乐(后台页面)")
	 public ResultMessage<PageData> findRecommendMusicByNameAndTypeId(String typeId,String name,Integer offset,Integer limit){
		 
		 logger.info("通过名字查询推荐音乐(后台页面)");
		 PageData page = new PageData();
		 page.setRows(musicrecommendService.findRecommendMusicByNameAndTypeId(typeId, name, offset, limit));
		 page.setTotal(musicrecommendService.countRecommendMusicByNameAndTypeId(typeId, name));
		 return ResultMessage.newSuccess("通过名字查询推荐音乐(后台页面)成功").setData(page);
	 }
	 
	 /**
	  * 将分类下的音乐全部推荐
	  * @param typeId
	  * @author lixs
	  * @Date 2017.11.15
	  */
	 @ResponseBody
     @RequestMapping(value = "/addAllRecommendMusic", method = {RequestMethod.GET, RequestMethod.POST})
     @ApiOperation(notes = "将分类下的音乐全部推荐", value = "将分类下的音乐全部推荐")
	 public ResultMessage<Boolean> addAllRecommendMusic(String typeId){
		 logger.info("将分类下的音乐全部推荐");
		 Boolean result = musicrecommendService.addAllRecommendMusic(typeId);
		 if(true==result){
			 return ResultMessage.newSuccess("推荐成功");
		 }
		 return ResultMessage.newFailure("推荐失败");
	 }
	 
	 
}
